package com.xish.api.shop;


import com.mysema.query.sql.dml.SQLUpdateClause;
import com.xish.api.shop.form.ShopEditForm;
import com.xish.model.bean.WzmShop;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jdbc.query.QueryDslJdbcTemplate;
import org.springframework.data.jdbc.query.SqlUpdateCallback;
import org.springframework.stereotype.Repository;

import static com.xish.model.table.QWzmShop.wzmShop;

@Repository
public class ShopRepository {

    @Autowired
    private QueryDslJdbcTemplate jdbcTemplate;

    public WzmShop findById(int id) {
        return jdbcTemplate.queryForObject(
                jdbcTemplate.newSqlQuery().from(wzmShop).where(wzmShop.id.eq(id)), wzmShop);
    }

    public WzmShop findByPoiId(int poiId) {
        return jdbcTemplate.queryForObject(
                jdbcTemplate.newSqlQuery().from(wzmShop).where(wzmShop.poiId.eq(poiId)), wzmShop);
    }

    public long updateByPoiId(final int poiId,final ShopEditForm form){
       return jdbcTemplate.update(wzmShop, new SqlUpdateCallback() {
            @Override
            public long doInSqlUpdateClause(SQLUpdateClause update) {
                return update.where(wzmShop.poiId.eq(poiId))
                        .set(wzmShop.city, Integer.parseInt(form.getCity_id()))
                        .set(wzmShop.tel,form.getPhone())
                        .set(wzmShop.summary,form.getSummary())
                        .set(wzmShop.title,form.getTitle())
                        .execute();
            }
        });
    }
}
